package com.anyou.mall.pla.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.StringUtils;
import com.anyou.mall.pla.domain.ShoVipTopUpInfo;
import com.anyou.mall.pla.mapper.ShoVipTopUpInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.pla.mapper.ShoVipTopUpMapper;
import com.anyou.mall.pla.domain.ShoVipTopUp;
import com.anyou.mall.pla.service.IShoVipTopUpService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 星级会员充值Service业务层处理
 *
 * @author xq
 * @date 2022-04-11
 */
@Service
public class ShoVipTopUpServiceImpl implements IShoVipTopUpService
{
    @Autowired
    private ShoVipTopUpMapper shoVipTopUpMapper;

    @Autowired
    private ShoVipTopUpInfoMapper shoVipTopUpInfoMapper;

    /**
     * 查询星级会员充值
     *
     * @param vipTopUpId 星级会员充值主键
     * @return 星级会员充值
     */
    @Override
    public ShoVipTopUp selectShoVipTopUpByVipTopUpId(Long vipTopUpId)
    {
        ShoVipTopUp shoVipTopUp = shoVipTopUpMapper.selectShoVipTopUpByVipTopUpId(vipTopUpId);
        shoVipTopUp.setShoVipTopUpInfo(shoVipTopUpInfoMapper.selectList(new LambdaQueryWrapper<ShoVipTopUpInfo>()
                .eq(ShoVipTopUpInfo::getVipTopUpId,shoVipTopUp.getVipTopUpId())));
        return shoVipTopUp;
    }

    /**
     * 查询星级会员充值列表
     *
     * @param shoVipTopUp 星级会员充值
     * @return 星级会员充值
     */
    @Override
    public List<ShoVipTopUp> selectShoVipTopUpList(ShoVipTopUp shoVipTopUp)
    {
        List<ShoVipTopUp> shoVipTopUps = shoVipTopUpMapper.selectShoVipTopUpList(shoVipTopUp);
        shoVipTopUps.forEach(e->{
            e.setShoVipTopUpInfo(shoVipTopUpInfoMapper.selectList(new LambdaQueryWrapper<ShoVipTopUpInfo>()
                    .eq(ShoVipTopUpInfo::getVipTopUpId,e.getVipTopUpId())));
        });

        return shoVipTopUps;
    }

    /**
     * 新增星级会员充值
     *
     * @param shoVipTopUpParam 星级会员充值
     * @return 结果
     */
    @Override
    @Transactional
    public int insertShoVipTopUp(ShoVipTopUp shoVipTopUpParam)
    {
        ShoVipTopUp shoVipTopUp = new ShoVipTopUp();
        BeanUtils.copyProperties(shoVipTopUpParam,shoVipTopUp);
        shoVipTopUp.setDelFlag("0");
        shoVipTopUpMapper.insert(shoVipTopUp);
        shoVipTopUpParam.getShoVipTopUpInfo().forEach(e->{
            e.setVipTopUpId(shoVipTopUp.getVipTopUpId());
            shoVipTopUpInfoMapper.insert(e);
        });

        return 1;
    }

    /**
     * 修改星级会员充值
     *
     * @param shoVipTopUp 星级会员充值
     * @return 结果
     */
    @Override
    @Transactional
    public int updateShoVipTopUp(ShoVipTopUp shoVipTopUp)
    {
        shoVipTopUp.setDelFlag("0");
        shoVipTopUpMapper.updateById(shoVipTopUp);
        shoVipTopUp.getShoVipTopUpInfo().forEach(e->{
            shoVipTopUpInfoMapper.deleteById(e.getVipTopUpInfoId());
        });
        shoVipTopUp.getShoVipTopUpInfo().forEach(e->{
            ShoVipTopUpInfo shoVipTopUpInfo = new ShoVipTopUpInfo();
            BeanUtils.copyProperties(e,shoVipTopUpInfo);
            shoVipTopUpInfo.setVipTopUpId(shoVipTopUp.getVipTopUpId());
            shoVipTopUpInfoMapper.insert(shoVipTopUpInfo);
        });
        return 1;
    }

    /**
     * 批量删除星级会员充值
     *
     * @param vipTopUpIds 需要删除的星级会员充值主键
     * @return 结果
     */
    @Override
    public int deleteShoVipTopUpByVipTopUpIds(Long[] vipTopUpIds)
    {
        return shoVipTopUpMapper.deleteShoVipTopUpByVipTopUpIds(vipTopUpIds);
    }

    /**
     * 删除星级会员充值信息
     *
     * @param vipTopUpId 星级会员充值主键
     * @return 结果
     */
    @Override
    public int deleteShoVipTopUpByVipTopUpId(Long vipTopUpId)
    {
        return shoVipTopUpMapper.deleteShoVipTopUpByVipTopUpId(vipTopUpId);
    }

    /**
     * 星级会员开始结束
     * @param shoVipTopUp
     * @return
     */
    @Override
    @Transactional
    public ApiResult activityStatus(ShoVipTopUp shoVipTopUp) {


        if (shoVipTopUp.getActivityStatus().equals("1")){
            List<ShoVipTopUp> shoVipTopUps = shoVipTopUpMapper.selectList(new LambdaQueryWrapper<ShoVipTopUp>()
                    .eq(ShoVipTopUp::getActivityStatus, 1)
                    .eq(ShoVipTopUp::getDelFlag, 0));
            for (ShoVipTopUp vipTopUp : shoVipTopUps) {
                shoVipTopUpMapper.update(null,new LambdaUpdateWrapper<ShoVipTopUp>()
                        .set(ShoVipTopUp::getActivityStatus,2)
                        .eq(ShoVipTopUp::getVipTopUpId,vipTopUp.getVipTopUpId()));
            }
        }

        return ApiResult.success(shoVipTopUpMapper.update(null,new LambdaUpdateWrapper<ShoVipTopUp>()
                .set(ShoVipTopUp::getActivityStatus,shoVipTopUp.getActivityStatus())
                .eq(ShoVipTopUp::getVipTopUpId,shoVipTopUp.getVipTopUpId())));
    }

    @Override
    public ApiResult getShopVip() {
        ShoVipTopUp shoVipTopUp = shoVipTopUpMapper.selectOne(new LambdaQueryWrapper<ShoVipTopUp>()
                .eq(ShoVipTopUp::getActivityStatus, 1));
        if (shoVipTopUp!=null){
            List<ShoVipTopUpInfo> shoVipTopUpInfos = shoVipTopUpInfoMapper.selectList(new LambdaQueryWrapper<ShoVipTopUpInfo>()
                    .eq(ShoVipTopUpInfo::getVipTopUpId, shoVipTopUp.getVipTopUpId()));
            shoVipTopUp.setShoVipTopUpInfo(shoVipTopUpInfos);
        }
        return ApiResult.success(shoVipTopUp);
    }
}
